package org.longs;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.longs.converter.WasConverter;
import org.longs.converter.WdfConverter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;

public class ResMain {
    private static String path = "E://sucai/";

    private static void isDirectory(File dir) throws IOException {
        if (dir.exists() && dir.isDirectory()) {
            for (File file : Objects.requireNonNull(dir.listFiles())) {
                isDirectory(file);
            }
        } else {
            final String parent = dir.getParent();
            final File file = new File(parent.replace("content", "skills"));
            if (file.exists()) {
                file.mkdirs();
            }
            System.out.println(file.getAbsolutePath().contains("头像"));
//            System.out.println(file.getAbsolutePath() + dir.getName());
            Actuator.wasConverterSpirit(dir.getAbsolutePath(), file.getAbsolutePath(), dir.getName(), !file.getAbsolutePath().contains("头像"));
        }
    }

    public static void main(String[] args) throws IOException {
        final File file = new File("src/main/resources/was");
        for (File listFile : file.listFiles()) {
            Actuator.wasConverterSpirit(listFile.getAbsolutePath(), "src/main/resources/content", listFile.getName(), false);
        }

//        final File file = new File(path);
//        for (File listFile : file.listFiles()) {
//            if (listFile.getName().contains("状态_")) {
//                final File dir = new File(listFile.getParent() + File.separator + "状态");
//                if (!dir.exists()) {
//                    dir.mkdir();
//                }
//                final String[] split = listFile.getName().split("_");
//                final File target = new File(dir.getAbsoluteFile() + File.separator + split[1]);
//                System.out.println(new File(dir.getAbsoluteFile() + File.separator + split[1]));
//                System.out.println(listFile);
//                if (!target.exists()) {
//                    Files.copy(listFile.toPath(), target.toPath());
//                }
//            }
//        }


//        isDirectory(new File(path + "content"));
//        final File dir = new File(path + "pet");
//        if (dir.exists()) {
//            final File target = new File(path + "content");
//            for (File file : dir.listFiles()) {
//                final File petDir = new File(target.getAbsolutePath() + File.separator + file.getName());
//                if (!petDir.exists()) {
//                    petDir.mkdir();
//                }
//                final File defaultDir = new File(target.getAbsolutePath() + File.separator + file.getName() + File.separator + "默认");
//                if (!defaultDir.exists()) {
//                    defaultDir.mkdir();
//                }
//
//                final File avatarDir = new File(file.getAbsolutePath() + File.separator + "头像");
//                if (avatarDir.exists()) {
//                    final File avatar = new File(petDir.getAbsolutePath() + File.separator + "头像");
//                    if (!avatar.exists()) {
//                        avatar.mkdir();
//                    }
//                }
//
//                final File jewelryDir = new File(file.getAbsolutePath() + File.separator + "饰品");
//                if (jewelryDir.exists()) {
//                    final File jewelry = new File(petDir.getAbsolutePath() + File.separator + "饰品");
//                    if (!jewelry.exists()) {
//                        jewelry.mkdir();
//                    }
//                }
//            }
//            for (File file : dir.listFiles()) {
//                for (File listFile : file.listFiles()) {
//                    if (listFile.getName().equals("饰品")) {
//                        for (File listedFile : listFile.listFiles()) {
//                            if (listedFile.getName().equals("饰品")) {
//                                continue;
//                            }
//                            final File targetFile = new File(target.getAbsolutePath() + File.separator + file.getName() + File.separator + listFile.getName() + File.separator + listedFile.getName());
//                            System.out.println(targetFile);
//                            final File sourceFile = new File(listedFile.getAbsolutePath());
//                            System.out.println(sourceFile);
//                            if (sourceFile.exists() && !targetFile.exists()) {
//                                Files.copy(sourceFile.toPath(), targetFile.toPath());
//                            }
//                        }
//                        continue;
//                    }
//                    if (listFile.getName().equals("头像")) {
//                        for (File listedFile : listFile.listFiles()) {
//                            String name = null;
//                            if (listedFile.getName().contains("头像")) {
//                                name = listedFile.getName().replace("头像", "对话头像");
//                            }
//                            if (listedFile.getName().contains("x32")) {
//                                name = listedFile.getName().replace("x32", "小头像");
//                            }
//                            if (listedFile.getName().contains("x44")) {
//                                name = listedFile.getName().replace("x44", "中头像");
//                            }
//                            final File targetFile = new File(target.getAbsolutePath() + File.separator + file.getName() + File.separator + listFile.getName() + File.separator + name);
//                            System.out.println(targetFile);
//                            final File sourceFile = new File(listedFile.getAbsolutePath());
//                            System.out.println(sourceFile);
//                            if (sourceFile.exists() && !targetFile.exists()) {
//                                Files.copy(sourceFile.toPath(), targetFile.toPath());
//                            }
//                        }
//                        continue;
//                    }
//                    final File targetFile = new File(target.getAbsolutePath() + File.separator + file.getName() + File.separator + "默认" + File.separator + listFile.getName());
//                        System.out.println(targetFile);
//                    final File sourceFile = new File(listFile.getAbsolutePath());
//                    System.out.println(sourceFile);
//                        if (sourceFile.exists() && !targetFile.exists()) {
//                        Files.copy(sourceFile.toPath(), targetFile.toPath());
//                    }
//                }
//            }
//        }
//        isDirectory(dir);
        //序列化json数据
//        JSONArray jsonData = getJsonData("texiao.json");
//        System.out.println(jsonData);
//        final List<TexiaoBean> list = new ArrayList<>();
//        for (Object jsonDatum : jsonData) {
//            final JSONObject datum = (JSONObject) jsonDatum;
//            final JSONArray object = (JSONArray) datum.get("content");
//            for (Object o : object) {
//                final JSONObject jsonObject = (JSONObject) o;
//                final TexiaoBean bean = new TexiaoBean();
//                if (jsonObject.get("x") instanceof Integer) {
//                    bean.setX(Double.valueOf((int) jsonObject.get("x") + ""));
//                } else {
//                    bean.setX(Double.valueOf((BigDecimal) jsonObject.get("x") + ""));
//                }
//                if (jsonObject.get("y") instanceof Integer) {
//                    bean.setY(Double.valueOf((int) jsonObject.get("y") + ""));
//                } else {
//                    bean.setY(Double.valueOf((BigDecimal) jsonObject.get("y") + ""));
//                }
//                bean.setName(jsonObject.get("切换") + "");
//                bean.setMapId((Integer) datum.get("mapId"));
//                list.add(bean);
//            }
//        }
//        for (TexiaoBean bean : list) {
//            Actuator.wasConverterSpirit("src/main/resources/was/" + bean.getName() + ".was", "src/main/resources/data/",bean.getName());
//        }
//////        获取wdf转换数据
//        List<WdfConverter> wdfConverters = getWdfConverters();
////        //
//        wdfConverterSpirit(jsonData, wdfConverters);
//        wasConverterAvatar();
//        wasConverterSpirit();
//        JSONObject jsonData = (JSONObject) getJsonData("item.json");
//        jsonData.forEach((key, item) -> {
////            System.out.println(key);
//            final JSONObject jsonObject = (JSONObject) item;
//            Object type = jsonObject.get("wp_7");
//            if (type != null) {
//                Integer level = (Integer) jsonObject.get("wp_5");
//                if (level != null && level == 10) {
//                    level = 0;
//                }
//                String index = (String) type + level;
//
////                System.out.println(index);
//                final File file = new File(path + "role");
//                for (File listFile : file.listFiles()) {
//                    if (listFile.getName().contains(index)) {
//                        listFile.renameTo(new File(listFile.getParent() + File.separator + listFile.getName().replace(index, type + "-" + key)));
//                    }
//                }
////                System.out.println(type);
//            }
//        });
//        JSONObject jsonData = (JSONObject) getJsonData("sound.json");
//        jsonData.forEach((key, item) -> {
//            final JSONObject jsonObject = (JSONObject) item;
//            if (jsonObject.get("文件") != null) {
//                final File file = new File(path + "content/skill/");
//                if (!file.exists()) {
//                    file.mkdir();
//                }
//                final Object object = jsonObject.get("文件");
//                String name = null;
//                if (object instanceof Long) {
//                    name = Long.toHexString((Long) object);
//                }
//
//                if (object instanceof Integer) {
//                    name = Long.toHexString((Integer) object);
//                }
//
//                final File wav = new File(path + "data/" + name + ".wav");
//                if (wav.exists()) {
//                    try {
//                        Files.copy(wav.toPath(), Paths.get(file.getAbsolutePath() + File.separator + key + ".wav"), StandardCopyOption.REPLACE_EXISTING);
//                    } catch (IOException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
//            } else {
//                File file = new File(path + "content/sound/");
//                if (!file.exists()) {
//                    file.mkdir();
//                }
//                file = new File(file.getAbsolutePath() + File.separator + key);
//                if (!file.exists()) {
//                    file.mkdir();
//                }
//                extracted(jsonObject, file, "防御");
//                extracted(jsonObject, file, "攻击");
//                extracted(jsonObject, file, "施法");
//                extracted(jsonObject, file, "死亡");
//                extracted(jsonObject, file, "挨打");
//                extracted(jsonObject, file, "暴击");
//            }
//        });
//        try (FileWriter  fos = new FileWriter (path + File.separator +"sound2.json")) {
//            fos.write(jsonData.toString()); // 将字节数组写入文件
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        System.out.println(jsonData);
    }

    private static void getSucai() throws IOException {
        final File dir = new File(path + "role");
        String[] actrions = new String[]{"挨打", "待机", "防御", "攻击", "施法", "死亡", "行走", "移动", "站立", "转身", "子弹"};
        String[] actrions5 = new String[]{"悲伤", "吃惊", "发怒", "舞蹈", "行礼", "休息", "招手"};
        String[] actrions1 = new String[]{"行走", "站立"};
        String[] actrions2 = new String[]{"x32", "x44", "背包", "头像"};
        String[] actrions3 = new String[]{"小头像", "中头像", "大头像", "对话头像"};
        for (File file : dir.listFiles()) {
            final String[] split = file.getName().split("-");
            final File content = new File(path + "content/" + split[0]);
            if (!content.exists()) {
                content.mkdir();
            }
            if (split.length == 2) {
                File typeDir = new File(content.getAbsolutePath() + File.separator + split[1]);
                if (!typeDir.exists()) {
                    typeDir.mkdir();
                }
                if (!split[1].equals("拥抱")) {
                    typeDir = new File(content.getAbsolutePath() + File.separator + split[1] + File.separator + "默认");
                    if (!typeDir.exists()) {
                        typeDir.mkdir();
                    }
                }
                File avatar = new File(content.getAbsolutePath() + File.separator + "头像");
                if (!avatar.exists()) {
                    avatar.mkdir();
                }
            }
            if (split.length == 3 && !split[1].equals("坐骑")) {
                File typeDir = new File(content.getAbsolutePath() + File.separator + split[1] + File.separator + split[2]);
                if (!typeDir.exists()) {
                    typeDir.mkdir();
                }
            }
            if (split[1].equals("坐骑")) {
                File typeDir = new File(content.getAbsolutePath() + File.separator + split[1]);
                if (!typeDir.exists()) {
                    typeDir.mkdir();
                }
                typeDir = new File(content.getAbsolutePath() + File.separator + split[1] + File.separator + split[2]);
                if (!typeDir.exists()) {
                    typeDir.mkdir();
                }
            }
        }

        for (File file : dir.listFiles()) {
            final String[] split = file.getName().split("-");
            final File content = new File(path + "content/" + split[0]);
            if (split[1].equals("坐骑")) {
                File typeDir = new File(content.getAbsolutePath() + File.separator + split[1] + File.separator + split[2]);
                for (String action : actrions1) {
                    final File copyFile = new File(typeDir.getAbsolutePath() + File.separator + action + ".was");
                    final File souresFile = new File(file.getAbsolutePath() + File.separator + action + ".was");
                    if (!copyFile.exists() && souresFile.exists()) {
                        Files.copy(souresFile.toPath(), copyFile.toPath());
                    }
                }
            }

            if (split.length == 3 && !split[1].equals("坐骑")) {
                File typeDir = new File(content.getAbsolutePath() + File.separator + split[1] + File.separator + split[2]);
                for (File listFile : file.listFiles()) {
                    final File copyFile = new File(typeDir.getAbsolutePath() + File.separator + listFile.getName());
                    System.out.println(copyFile.getAbsolutePath());
                    if (!copyFile.exists() && listFile.exists()) {
                        Files.copy(listFile.toPath(), copyFile.toPath());
                    }
                }
            }
            if (split.length == 2) {
                File typeDir = new File(content.getAbsolutePath() + File.separator + split[1]);
                File avatar = new File(content.getAbsolutePath() + File.separator + "头像");
                if (!split[1].equals("拥抱")) {
                    for (String actrion : actrions) {
                        final File copyFile = new File(typeDir.getAbsolutePath() + File.separator + "默认" + File.separator + actrion + ".was");
                        final File souresFile = new File(file.getAbsolutePath() + File.separator + actrion + ".was");
                        if (!copyFile.exists() && souresFile.exists()) {
                            Files.copy(souresFile.toPath(), copyFile.toPath());
                        }
                    }
                    for (int i = 0; i < actrions2.length; i++) {
                        final File souresFile = new File(file.getAbsolutePath() + File.separator + "头像" + File.separator + actrions2[i] + ".was");
                        final File copyFile = new File(avatar.getAbsolutePath() + File.separator + File.separator + actrions3[i] + ".was");
                        if (!copyFile.exists() && souresFile.exists()) {
                            Files.copy(souresFile.toPath(), copyFile.toPath());
                        }
                    }
                    for (String actrion : actrions5) {
                        final File copyFile = new File(typeDir.getAbsolutePath().substring(0, typeDir.getAbsolutePath().length() - split[1].length()) + "拥抱\\" + File.separator + actrion + ".was");
                        final File souresFile = new File(file.getAbsolutePath() + File.separator + actrion + ".was");
                        if (!copyFile.exists() && souresFile.exists()) {
                            Files.copy(souresFile.toPath(), copyFile.toPath());
                        }
                    }
                } else {
                    for (String actrion : actrions1) {
                        final File copyFile = new File(typeDir.getAbsolutePath() + File.separator + actrion + ".was");
                        final File souresFile = new File(file.getAbsolutePath() + File.separator + actrion + ".was");
                        if (!copyFile.exists() && souresFile.exists()) {
                            Files.copy(souresFile.toPath(), copyFile.toPath());
                        }
                    }
                }
            }
        }
    }

    private static void extracted(JSONObject jsonObject, File file, String key) {
        final Object object = jsonObject.get(key);
        String name = null;
        if (object instanceof Long) {
            name = Long.toHexString((Long) object);
        }

        if (object instanceof Integer) {
            name = Long.toHexString((Integer) object);
        }
        final File wav = new File(path + "data/" + name + ".wav");
        if (wav.exists()) {
            try {
                Files.copy(wav.toPath(), Paths.get(file.getAbsolutePath() + File.separator + key + ".wav"), StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private static void update(JSONObject jsonObject, String key) {
        Object object = jsonObject.get(key);
        if (object != null) {
            if (object instanceof Long) {
                jsonObject.fluentPut(key, Long.toHexString((Long) object));
            }

            if (object instanceof Integer) {
                jsonObject.fluentPut(key, Long.toHexString((Integer) object));
            }
        }
    }

    private static void wasConverterSpirit() throws IOException {
        final File file = new File(path + "data");
        if (!file.exists()) {
            if (!file.mkdir()) {
                System.out.println("创建" + file.getName() + "失败");
            }
        }
        final File dir = new File(path + "was2");
        if (dir.exists()) {
            for (File listFile : Objects.requireNonNull(dir.listFiles())) {
                Actuator.wasConverterSpirit(listFile.getAbsolutePath(), file.getAbsolutePath(), listFile.getName());
            }
        }
    }

    private static void wasConverterAvatar() throws IOException {
        final File file = new File(path + "data");
        if (!file.exists()) {
            if (!file.mkdir()) {
                System.out.println("创建" + file.getName() + "失败");
            }
        }
        final File dir = new File(path + "was2");
        if (dir.exists()) {
            for (File listFile : Objects.requireNonNull(dir.listFiles())) {
                if (!listFile.getName().contains("站立")) {
                    continue;
                }
                WasConverter wasConverter = new WasConverter(listFile.getAbsolutePath());
                wasConverter.analysis();
                try {
                    wasConverter.converterBufferedImage(wasConverter.getImageFiles().get(0));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                Actuator.converterAvatar(file.getAbsolutePath(), listFile.getName().replace("_站立.was", ""), wasConverter);
            }
        }
    }


    private static void wdfConverterSpirit(JSONObject jsonData, List<WdfConverter> wdfConverters) throws IOException {
        final File file = new File(path + "data");
        if (!file.exists()) {
            if (!file.mkdir()) {
                System.out.println("创建" + file.getName() + "失败");
            }
        }
        for (Map.Entry<String, Object> entry : jsonData.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            final JSONObject jsonObject = (JSONObject) value;
            String keyName = null;
            boolean flag = true;
            for (Map.Entry<String, Object> mapEntry : jsonObject.entrySet()) {
                String name = mapEntry.getKey();
                Object index = mapEntry.getValue();
                if ("wp_11".equals(name)) {
                    keyName = (String) index;
                }
                if ("wp_12".equals(name)) {
                    flag = wdf(wdfConverters, file, key + "_小图标", name, index);
                }
                if ("wp_13".equals(name)) {
                    flag = wdf(wdfConverters, file, key + "_大图标", name, index);
                }
            }
            if (flag) {
                System.out.println("找不到" + keyName);
            }
        }
    }

    private static boolean wdf(List<WdfConverter> wdfConverters, File file, String key, String name, Object index) throws IOException {


        for (WdfConverter converter : wdfConverters) {
            String hexString = null;
            if (index instanceof Integer) {
                hexString = Long.toHexString((Integer) index);
            }
            if (index instanceof Long) {
                hexString = Long.toHexString((Long) index);
            }
            final WasConverter wasConverter = converter.getWasConverterMap().get(hexString);
            if (wasConverter == null) {
                continue;
            }
            wasConverter.analysis();
            try {
                wasConverter.converterBufferedImage(wasConverter.getImageFiles().get(0));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            Actuator.converterAvatar(file.getAbsolutePath(), key, wasConverter);
            return false;
        }
        return true;

    }

    private static List<WdfConverter> getWdfConverters() {
        final File file = new File(path + "wdf");
        final ArrayList<WdfConverter> list = new ArrayList<>();
        if (file.exists()) {
            final File[] files = file.listFiles();
            for (File file1 : files) {
                final WdfConverter wdfConverter = new WdfConverter(file1.getAbsolutePath());
                wdfConverter.analysis();
                list.add(wdfConverter);

            }
        }
        return list;
    }

    private static Object getJsonData(String data) {
        final File file = new File(path + data);
        if (file.exists()) {
            final StringBuffer buffer = new StringBuffer();
            try {
                Scanner scanner = new Scanner(new FileInputStream(file), "UTF-8");
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    buffer.append(line);
                }
                scanner.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return JSON.parse(buffer.toString());
        }
        return null;
    }

}
